// Package _8__最长不含重复字符的子字符串 main
// @Title _8__最长不含重复字符的子字符串
// @Description
// @Author  == 琳琳也叫王木木 ==  2022-02-15 9:19
// @Create  2022-02-15 9:19
package main

import "fmt"

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

/**
求字符串中最长无重复字符串的长度
*/
func lengthOfLongestSubstring(s string) int {
	if len(s) == 0 {
		return 0
	}
	var ans int
	l, r := 0, -1
	hashMap := make(map[byte]bool)
	for i := 0; i < len(s); i++ {
		if _, v := hashMap[s[i]]; !v {
			hashMap[s[i]] = true
			r = i
			continue
		}
		for l <= r {
			if _, v := hashMap[s[l]]; v {
				delete(hashMap, s[l])
				l++
			}
			break
		}
		ans = max(ans, r-l)
	}
	return ans
}

func main() {
	//fmt.Println(lengthOfLongestSubstring("abcabcbb"))
	//fmt.Println(lengthOfLongestSubstring("bbbbb"))
	fmt.Println(lengthOfLongestSubstring("pwwkew"))
}
